﻿using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace isky.Plugin
{
   public class UpdatePostYGCBPlugin : IPlugin
    {
      
        /// <summary>
        /// 报价利润修改触发事件
        /// </summary>
        /// <param name="serviceProvider"></param>
        public void Execute(IServiceProvider serviceProvider)
        {
              CommonWcf commonWcf = new CommonWcf();
        SalesOrderYGCBSql salesOrderYGCBSql = new SalesOrderYGCBSql();
            // Obtain the execution context service from the service provider.
            var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            // Obtain the Organization Service factory service from the service provider
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var service = factory.CreateOrganizationService(context.UserId);
            try
            {
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity inputEntity = context.InputParameters["Target"] as Entity;
                    Entity PreImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains("PreImage")) ? context.PreEntityImages["PreImage"] : null;
                    if (inputEntity.Id == Guid.Empty)
                    {
                        return;
                    }
                    if (inputEntity.Contains("new_usdcost"))
                    {
                        EntityReference caseReference = commonWcf.GetAttributeInputOrImage<EntityReference>(inputEntity, PreImageEntity, "new_case", null);
                        if (caseReference != null)
                        {
                            Entity orderentity = new Entity("salesorder", caseReference.Id);
                            decimal totalus = salesOrderYGCBSql.GetYGCBInfo(service, caseReference.Id, Guid.Empty, "Update");
                            orderentity.Attributes["new_totalprofit"] = totalus;
                            if (totalus == 0)
                            {
                                orderentity.Attributes["new_totalprofitenglish"] = "ZERO";
                                orderentity.Attributes["new_totalprofitcapital"] = "零元整";
                            }
                            else
                            {
                                string totalusEnglish = salesOrderYGCBSql.NumberToString((double)totalus);
                                orderentity.Attributes["new_totalprofitenglish"] = totalusEnglish.ToUpper();

                                string totalusToUppe = salesOrderYGCBSql.MoneyToUpper(totalus.ToString());
                                orderentity.Attributes["new_totalprofitcapital"] = totalusToUppe;
                            }
                            Entity entity = service.Retrieve("salesorder", caseReference.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_time"));
                            DateTime dateTime = entity.GetAttributeDefaultValue<DateTime>("new_time", DateTime.MinValue);
                            if (dateTime != DateTime.MinValue)
                            {
                                orderentity.Attributes["new_shipmentdate"] = dateTime.GetDateTimeFormats('D')[0].ToString();
                            }
                            service.Update(orderentity);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
                throw new NotImplementedException();
            }
        }
    }
}
